package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.ExceptionLogMapper;
import com.itheima.pojo.ExceptionLog;
import com.itheima.service.ExceptionLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {

    @Autowired
    private ExceptionLogMapper exceptionLogMapper;

    @Override
    public Page<ExceptionLog> getExceptionList(Map<String, Object> searchMap) {
        //获取当前页
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if(null == currentPage || currentPage < 0){
            currentPage = 0;
        }
        //设置默认每页显示条数
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (null == pageSize || pageSize <= 0) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage,pageSize);
        Example example = this.createExample(searchMap);
        Page<ExceptionLog> page = (Page<ExceptionLog>) exceptionLogMapper.selectByExample(example);
        return page;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(ExceptionLog.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andLike("uid","%"+searchMap.get("uid")+"%");
            }
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andLike("exceptionJson","%"+searchMap.get("exceptionJson")+"%");
            }
            if(searchMap.get("exceptionMessage")!=null && !"".equals(searchMap.get("exceptionMessage"))){
                criteria.andLike("exceptionMessage","%"+searchMap.get("exceptionMessage")+"%");
            }
            if(searchMap.get("ip")!=null && !"".equals(searchMap.get("ip"))){
                criteria.andLike("ip","%"+searchMap.get("ip")+"%");
            }
            if(searchMap.get("ipSource")!=null && !"".equals(searchMap.get("ipSource"))){
                criteria.andLike("ipSource","%"+searchMap.get("ipSource")+"%");
            }
            if(searchMap.get("method")!=null && !"".equals(searchMap.get("method"))){
                criteria.andLike("method","%"+searchMap.get("method")+"%");
            }
            if(searchMap.get("operation")!=null && !"".equals(searchMap.get("operation"))){
                criteria.andLike("operation","%"+searchMap.get("operation")+"%");
            }
            if(searchMap.get("params")!=null && !"".equals(searchMap.get("params"))){
                criteria.andLike("params","%"+searchMap.get("params")+"%");
            }
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
            if (searchMap.get("startTime")!=null){
                criteria.andGreaterThan("createTime",searchMap.get("startTime"));
            }
            if (searchMap.get("keyword")!=null){
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("keyword"));
            }


        }
        return example;
    }
}
